import { _BasicDataSource } from "../../_BasicDataSource"
import { uuid } from '../../util'

import { BaseEntity } from "../Entitys/BaseEntity";
import { Label } from "../Entitys/_Label";
import { Arrow } from '../Entitys/_Arrow'
import { COLOR, GetCenter, formarColor } from "../../Config";


class ArrowEntity {
    constructor(PlatformA, PlatformB, parent) {
        this.id = 'ArrowEntity_' + PlatformA.id + '_' + PlatformB.id;
        this.parent = parent;

        this.PlatformA = PlatformA;
        this.PlatformB = PlatformB;

        this.options = {
            ArcType: parent.options.ArcType || Cesium.ArcType.NONE,
            width: parent.options.width == null || parent.options.width == undefined ? 0 : 1,
            color: parent.options.color,
            name: {
                text: parent.options.text || '',
                color: parent.options.textColor || '#ffffff',
                background: parent.options.color || '#ffffff',
            },
        }

        this.EntityTypeName = 'LinkEntity';
        // 基础平台类型
        this.BaseEntity = new BaseEntity({});
        this.BaseEntity.CesiumPosition = GetCenter([this.PlatformA.BaseEntity.CesiumPosition, this.PlatformB.BaseEntity.CesiumPosition]);

        // 显示名称类型
        this.Label = new Label(this.BaseEntity.Entity, this.options.name);
        this.Link = new Arrow(this.BaseEntity.Entity, this.PlatformA.BaseEntity.CesiumPosition, this.PlatformB.BaseEntity.CesiumPosition, parent.options)

        // 输出用于图层加载的对象
        this.Entity = this.BaseEntity.Entity;

        this.PlatformA.addLink(this);
        this.PlatformB.addLink(this);
    }
    setPosition() {
        this.BaseEntity.CesiumPosition = GetCenter([this.PlatformA.BaseEntity.CesiumPosition, this.PlatformB.BaseEntity.CesiumPosition]);
        this.Link.setPosition(this.PlatformA.BaseEntity.CesiumPosition, this.PlatformB.BaseEntity.CesiumPosition);
    }
    destroy() {
        this.PlatformA.removeLink(this);
        this.PlatformB.removeLink(this);
    }
    set Color(value) {
        var _color = formarColor(value);
        this.Entity.polyline.material = new Cesium.PolylineArrowMaterialProperty(_color);
        this.Entity.label && (this.Entity.label.backgroundColor = _color)
    }
    get Color() {
        return this.parent.options.color
    }
}



class ArrowDataSource extends _BasicDataSource {
    constructor(parent, options, parentDataSource) {
        super(uuid(), parent, options);
        this.parentDataSource = parentDataSource;
        this.options = options || {};
        // this.options.LabelStatus = options.LabelStatus === undefined ? true : options.LabelStatus;
    }

    /**
     * 此处覆盖了父级的 Adds 方法，自行实现对应内容
     *
     * @param {*} platformArray
     * @memberof LinkDataSource
     */
    Adds(linkArray) {
        this._Entities.suspendEvents();
        // 获取现有的线的集合
        var linkKeyArray = Object.keys(this._Mapping);
        // 获取已经数据中已经存在的显得集合
        var hasKeyArray = [];
        // 遍历数据源
        linkArray.forEach(item => {
            var ids = item[0] + '_' + item[1];
            if (!this._Mapping[ids]) {
                // 从父级图层中获取对应的平台元素数据源
                var A = this.parentDataSource.GetMappingById(item[0]);
                var B = this.parentDataSource.GetMappingById(item[1]);
                // 如果两个数据源都存在，则创建，否则跳过，不进行操作
                if (A && B) {
                    const _LinkEntity = new ArrowEntity(A, B, this);
                    hasKeyArray.push(ids);
                    this._Mapping[ids] = _LinkEntity;
                    this._Entities.add(_LinkEntity.Entity);
                }
            } else {
                // 不管正向或者反向，均进行记录
                hasKeyArray.push(ids);
            }
        });
        // 进行数据源去重，删除已经存在的，和新创建的连接线对象ID
        hasKeyArray.forEach((item) => {
            if (linkKeyArray.includes(item)) {
                var index = linkKeyArray.indexOf(item);
                linkKeyArray.splice(index, 1);
            }
        });
        // 删除元素
        linkKeyArray.forEach(item => {
            this.Remove(item);
        });
        this._Entities.resumeEvents();
    }
    set Color(value) {
        this.EachMapping((item) => {
            item.Color = value;
        })
    }
    get Color() {
        return this.options.color
    }
    // set LabelStatus(value) {
    //     this.options.defaultNameStatus = value;
    //     this.EachMapping(item => {
    //         item.Label.show = !!value;
    //     });
    // }
    // get LabelStatus() {
    //     return this.options.defaultNameStatus;
    // }
    showLabel(type) {
        this.EachMapping(item => {
            item.Label.show = !!type;
        });
    }

}

export {
    ArrowEntity,
    ArrowDataSource
}
